package com.xb.cloud.merchantapi.adapter;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.xb.cloud.common.util.DateUtils;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.merchantapi.product.service.StatementApiService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;

/**
 * 报表数据查询接口
 * @ClassName:  StatementApiAdapter   
 * @Description:TODO
 * @author: 鲜备科技
 * @date:   2018年4月10日 下午3:05:32   
 *     
 * @Copyright: 2018 www.xianbeitech.com Inc. All rights reserved. 
 *
 */

@Component
public class StatementApiAdapter extends Adapter {
	private final Logger logger = LoggerFactory.getLogger(StatementApiAdapter.class);

	@Resource
	StatementApiService statementApiService;

	/**
	 * 
	 * <p>
	 * Title: execute
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param urlPath
	 * @param isLogined
	 * @param bizInfo
	 * @param context
	 * @return
	 * @see com.xb.cloud.gateway.client.Adapter#execute(java.lang.String,
	 *      java.lang.Boolean, java.util.Map,
	 *      com.xb.cloud.gateway.client.AdapterContext)
	 */
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		long time = System.currentTimeMillis();

		RtnInfo rtnInfo = null;
		long companyId = context.getCompanyId();
		Long endMerchantUserId = context.getCurrentMerchantUserId();
		
		String startTimeString = bizInfo.get("startTime");
		String endTimeString = bizInfo.get("endTime");
		Date startTime = null;
		Date endTime = null;
		if (startTimeString == null && endTimeString == null) {
			Calendar calendar = Calendar.getInstance();
			endTime = calendar.getTime();
			calendar.setTime(new Date());
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			Date zero = calendar.getTime();
			startTime = DateUtils.addDays(zero, -6);

		} else if (startTimeString != null && endTimeString != null) {
			try {
				startTime = DateUtils.stringToDate(startTimeString, DateUtils.patternA);
				endTime = DateUtils.stringToDate(endTimeString, DateUtils.patternA);
			} catch (Exception e) {
				return ReturnUtils.error(ReturnCodes.STATEMENT_QUERY_DATE_ILLEGAL, "query date illegal");
			}
			
		} else {
			return ReturnUtils.error(ReturnCodes.STATEMENT_QUERY_DATE_ILLEGAL, "query date illegal");
		}

		
        if("/h5/merchant/statement/getTradeInfo".equalsIgnoreCase(urlPath)) {
        	EndMerchantUserBo merchantUserBo = new EndMerchantUserBo();
			merchantUserBo.setCompanyId(companyId);
			merchantUserBo.setId(endMerchantUserId);
			
			
			return statementApiService.getTradeInfo(merchantUserBo, startTime, endTime);
        }
		logger.debug("执行时间：{}", System.currentTimeMillis() - time);
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/merchant/statement/**";
	}

	@Override
	public boolean isCheckUserToken() {
		return true;
	}
}
